package cn.samler.diandi.api.utils;

import cn.samler.diandi.api.entity.User;

import java.util.HashMap;

/**
 * 用户工具类
 *
 * @author samler
 */
public class UserUtil {

    /**
     * 获取登录成功后的access_token
     * @param user 用户实体
     * @return accessToken
     * @author samler
     */
    public static String getAccessToken(User user) {
        return JWTUtil.getInstance().sign(new HashMap<String, Object>(){
            {
                this.put("uid", user.getId());
                this.put("token", getToken(user));
            }
        });
    }

    /**
     * 获取登录成功后的access_token的Map
     * @param user 用户实体
     * @return HashMap
     * @author samler
     */
    public static HashMap<String, Object> getAccessTokenMap(User user) {
        return new HashMap<String, Object>(){
            {
                this.put("access_token", getAccessToken(user));
            }
        };
    }

    /**
     * 获取用户的token
     * @param user 用户实体
     * @return String
     * @author samler
     */
    public static String getToken(User user) {
        return PasswordUtils.getMD5("19990820-" + user.getId() + "@" + user.getPassword());
    }

}
